<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生成身份证号</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no,minimal-ui" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />   
<meta name="format-detection" content="email=no" />
</head>
<body>
<h3>生成身份证</h3>
	<form method="post" id="formId">
		年：<input type="text" name="year" id="year" value="1980"><br/>
		月：<input type="text" name="month" id="month" value="01"><br/>
		日：<input type="text" name="day" id="day" value="01"><br/>
		性别：
			<input type="radio" name="sex" value="1" checked/>男
			<input type="radio" name="sex" value="0"/>女
		<br/>
		<input type="button" onclick="subclick()" value="生成">
	</form>
	<hr/>
	<span id="card"></span>
</body>
<script type="text/javascript">
/*
 *      
 *          ┌─┐       ┌─┐
 *       ┌──┘ ┴───────┘ ┴──┐
 *       │                 │
 *       │       ───       │
 *       │  ─┬┘       └┬─  │
 *       │                 │
 *       │       ─┴─       │
 *       │                 │
 *       └───┐         ┌───┘
 *           │         │
 *           │         │
 *           │         │
 *           │         └──────────────┐
 *           │                        │
 *           │                        ├─┐
 *           │                        ┌─┘    
 *           │                        │
 *           └─┐  ┐  ┌───────┬──┐  ┌──┘         
 *             │ ─┤ ─┤       │ ─┤ ─┤         
 *             └──┴──┘       └──┴──┘ 
 *                  草泥马
 *                  
 */
	
	function subclick(){
		var card = getCard();
		document.getElementById("card").innerHTML = card;
	}
	function getCard(){
		var year = document.getElementById("year").value;
		var month = document.getElementById("month").value;
		var day = document.getElementById("day").value;
		var radios = document.getElementsByName("sex");
		var sex = '';
		for(var i = 0 ; i< radios.length; i++){
			var radio = radios.item(i);
			if(radio.checked){
				sex = radio.value;
			}
		}
		sex = jiou(sex);
		var place = 110101;
		var a = Math.floor(10*Math.random());//第15位
		var b = Math.floor(10*Math.random());//第16位
		var one = Math.floor(place/100000);
    	var two = Math.floor(place/10000)%10;
    	var three = Math.floor(place%10000/1000);
    	var four = Math.floor(place%1000/100);
    	var five = Math.floor(place%100/10);
    	var six = place%10;
    	var year1 = Math.floor(year/1000);
    	var year2 = Math.floor(year/100)%10;
    	var year3 = Math.floor(year/10)%10;
    	var year4 = year%10;
    	var month1= Math.floor(month/10);
    	var month2= month%10;
    	var day1 = Math.floor(day/10);
    	var day2 = day%10;
    	var sum = one*7
				+ two*9
				+ three*10
				+ four*5
				+ five*8
				+ six*4
				+ year1*2
				+ year2*1
				+ year3*6
				+ year4*3
				+ month1*7
				+ month2*9
				+ day1*10
				+ day2*5
				+ a*8
				+ b*4
				+ sex*2;
    	var c = sum%11;
    	var d = '';
    	switch(c){
    	case 0:
    		d = "1";
    		break;
    	case 1:
    		d = "0";
    		break;
    	case 2:
    		d = "X";
    		break;
    	case 3:
    		d = "9";
    		break;
    	case 4:
    		d = "8";
    		break;
    	case 5:
    		d = "7";
    		break;
    	case 6:
    		d = "6";
    		break;
    	case 7:
    		d = "5";
    		break;
    	case 8:
    		d = "4";
    		break;
    	case 9:
    		d = "3";
    		break;
    	case 10:
    		d = "2";
    		break;
    	}
    	var card = place + year + month + day + a + b + sex + d;
    	return card;
	}
	//获取奇数/偶数  sex=1返回奇数，sex=0返回偶数
	function jiou(sex){
		var a = Math.floor(10*Math.random());
		while(a%2 != sex){
			a = Math.floor(10*Math.random());
		}
		return a;
	}
	
</script>
</html>
